extends KinematicBody

#13.5
export var max_speed = 13.5
#10
export var acceleration = 10

export var turn_acceleration = 12

export var max_speed_back = 180
export var acceleration_back = 90

#0.004
export var turn_speed = 3.3

export var gravity = -21
var velocity = Vector3(0, 0, 0)
var direction = Vector3(0, 0, 0)
var current_turn = 0

onready var wall_ray_cast = get_node("CollisionShape/WallRayCast")
onready var floor_ray_cast = get_node("CollisionShape/FloorRayCast")
onready var player_camera = get_node("PlayerCamera")

var camera_angle = 0

var player_control_enabled

var was_on_wall

#used for debugging now
var jump_count = 0
var inputEventMouseMove = null
var is_on_ground


func _ready():
	player_control_enabled = true
	was_on_wall = false
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func enable_player_control(enable):
	player_control_enabled	= enable
	
func _on_game_started():
	player_control_enabled = true

	
func _input(event):	
	if event is InputEventMouseMotion:
		inputEventMouseMove = event
	
	
func walk(delta):

	var aim = global_transform.basis
	direction = Vector3()
	
	#if floor_ray_cast.is_colliding():
	#	print ("floor collision!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
	#else:
	#	print ("no floor collision")
	#if wall_ray_cast.is_colliding():
	#	print ("wall collision#################################")
	
	if  !wall_ray_cast.is_colliding() or floor_ray_cast.is_colliding(): 
		if Input.is_action_pressed("ui_up"):
			direction.x = direction.x - aim.y.x
			direction.z = direction.z - aim.y.z
			
		elif Input.is_action_pressed("ui_down"):
			direction.x = direction.x + aim.y.x
			direction.z = direction.z + aim.y.z
		
		if Input.is_action_pressed("ui_left"):
			direction.x = direction.x - aim.x.x
			direction.z = direction.z - aim.x.z
		
		if Input.is_action_pressed("ui_right"):
			direction.x = direction.x + aim.x.x
			direction.z = direction.z + aim.x.z
	
	

	
	direction.y = 0
	direction = direction.normalized()
	
	if (is_on_floor()):
		is_on_ground = true
	else:
		if !floor_ray_cast.is_colliding():
			is_on_ground = false
	
	if (is_on_ground and !is_on_floor()):
		move_and_collide(Vector3(0, -1, 0))
	
	velocity.y += gravity * delta
	
	var temp_velocity = velocity
	temp_velocity.y = 0
	
	var target = direction * max_speed
	
	var accel
	if direction.dot(temp_velocity) > 0:
		accel = 8
	else:
		accel = 16
	if wall_ray_cast.is_colliding() and !floor_ray_cast.is_colliding() and velocity.y > 0:
		accel = 20
			
	temp_velocity = temp_velocity.linear_interpolate(target, accel * delta)
	velocity.x = temp_velocity.x
	velocity.z = temp_velocity.z
	#print (velocity.length())
	
	velocity = move_and_slide(velocity, Vector3(0, 1, 0), 0.9, 4, 0.75)
	#velocity = move_and_slide(velocity, Vector3(0, 1, 0))
	
	
func turn(delta):
	if inputEventMouseMove != null:
		var speed_factor = min(abs(inputEventMouseMove.relative.x), 3.6)
		var v_speed_factor = min(abs(inputEventMouseMove.relative.y), 3.6)
		current_turn = ((inputEventMouseMove.relative.x * -turn_speed) + (inputEventMouseMove.relative.x * -speed_factor * 0.4)) * delta
		#print (current_turn)
		var change = ((inputEventMouseMove.relative.y * -turn_speed) + (inputEventMouseMove.relative.y * -speed_factor * 0.4)) * delta
		if change + camera_angle < 90 and change + camera_angle > -90:
			player_camera.rotate_x(deg2rad(change))
			camera_angle += change
			
	else:
		current_turn = current_turn * 0.88
		
	rotate_y(deg2rad(current_turn))	
	
	inputEventMouseMove = null

func jump(delta):
	if player_control_enabled:
		var jump_height = 22

		if is_on_floor() and Input.is_action_pressed("jump"):
			velocity.y = jump_height
			jump_count = jump_count + 1
			get_node("/root/Save").save_node_data(self)


func _physics_process(delta):
	walk(delta)
	turn(delta)
	jump(delta)
	
	
func save():
	var save_dict = {
		jump_count = jump_count,
	}
	return save_dict
